/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.haipv.gtkhoa501.screen;

import com.haipv.gtkhoa501.controller.Event;
import com.haipv.gtkhoa501.model.Signs;
import com.haipv.gtkhoa501.model.Var;
import com.haipv.gtkhoa501.util.Resize;
import com.haipv.gtkhoa501.util.ResizeLcd;
import com.nokia.lwuit.components.HeaderBar;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.GenericListCellRenderer;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Ticker;

/**
 *
 * @author PHANHAI
 * Giao diện hiển thị danh sách các biển báo.
 */
public class ListSignsScreen extends BaseScreen implements ActionListener,CommandListener {

    List list = null;
    private DefaultListModel model;
    String title;
    Vector items;
    Command back = new Command(Var.back);
    javax.microedition.lcdui.Command cmdBack= new javax.microedition.lcdui.Command(Var.back, javax.microedition.lcdui.Command.BACK,0);
    javax.microedition.lcdui.List listLcd=null;
    
    public ListSignsScreen(String title, Vector items) {
        this.title = title;
        this.items = items;
    }

    public void showForm() {
        listLcd= new javax.microedition.lcdui.List(title, javax.microedition.lcdui.List.IMPLICIT);
        Signs sign;
        ResizeLcd re= new ResizeLcd();
        int n = items.size();
        for (int i = 0; i < n; i++) {
            Command item = null;
            sign = (Signs) items.elementAt(i);
            try {
                listLcd.append(sign.getTitle(), re.CreateScaledImage(javax.microedition.lcdui.Image.createImage(Var.linkSigns + sign.getImg() + ".png"),30,30));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        listLcd.setFitPolicy(javax.microedition.lcdui.List.TEXT_WRAP_OFF);
        
        
        listLcd.addCommand(cmdBack);
        listLcd.setCommandListener(this);
        javax.microedition.lcdui.Display.getDisplay(controller.main).setCurrent(listLcd);
        
    }
     private Container createGenericRendererContainer() {
        Font font = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);

        Container f = new Container(new BorderLayout());
        
        Label labImg = new Label();
        labImg.setName("nameImg");
        ResizeLcd re= new ResizeLcd();
       // Label labImg = null;
        try {
            labImg = new Label(Image.createImage(re.CreateScaledImage(javax.microedition.lcdui.Image.createImage(labImg.getText()),30,30)));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        Label bank = new Label();
        bank.setName("data");
        bank.setFocusable(true);
        bank.getStyle().setBgTransparency(0);
        bank.getStyle().setFgColor(0x000000);
        bank.setEndsWith3Points(true);

        f.addComponent(BorderLayout.WEST,labImg);
        f.addComponent(BorderLayout.EAST,bank);

        return f;
    }

    private Hashtable[] createGenericListCellRendererModelData() {
        Hashtable[] data = new Hashtable[items.size()];
        Signs sign;
        for (int i = 0; i < items.size(); i++) {
            data[i] = new Hashtable();
            sign = (Signs) items.elementAt(i);
            data[i].put("nameImg", Var.linkSigns + sign.getImg() + ".png");
            data[i].put("data", sign.getTitle());
        }

        return data;
    }
    
    public void showForm1() {
//        Label a = new Label();
//        a.setEndsWith3Points(focusScrolling);
        Form form = new Form();
        form.setScrollableY(false);
        HeaderBar header = null;
        try {
            header = new HeaderBar(title);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        header.setHeaderTitleColor(Var.colorHearder);
        header.getStyle().setBgColor(Var.colorWhite);
        header.setScrollable(false);

        form.addComponent(header);

        /*
        model = new DefaultListModel();

        int n = items.size();
        Signs sign;
        Resize re= new Resize();
        for (int i = 0; i < n; i++) {
            Command item = null;
            sign = (Signs) items.elementAt(i);
            try {
                item = new Command(sign.getTitle(), re.resizeImage(Image.createImage(Var.linkSigns + sign.getImg() + ".png"),30,30));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            if (null != item) {
                model.addItem(item);
            }
        }
        list = new List(model);
        list.addActionListener(this);
        
        //list.getStyle().setPadding(0, 0, 10, 10);
         */ 
        
        list = new List(createGenericListCellRendererModelData());
            list.setRenderer(new GenericListCellRenderer(createGenericRendererContainer(),
                    createGenericRendererContainer()));
        list.addActionListener(this);
        Container con = new Container();
        con.addComponent(list);
        con.setScrollableY(true);
        form.addComponent(con);
        form.setBackCommand(back);
        form.addCommand(back);
        form.addCommandListener(this);
        
        form.show();
    }

    public void actionPerformed(ActionEvent ae) {

        if (ae.getCommand() == back) {
            controller.handleEvent(Event.SIGN_VIEW, null);
        } else {
            if (ae.getComponent() == list) {
                Event event = new Event();
                event.setKey("ID", ""+((Signs)items.elementAt(list.getSelectedIndex())).getId());
                controller.handleEvent(Event.SIGN_DETAIL, event);
            }
        }
    }

    public void commandAction(javax.microedition.lcdui.Command c, Displayable d) {
         if (c == cmdBack) {
            controller.handleEvent(Event.SIGN_VIEW, null);
        } else {
            if (d == listLcd) {
                Event event = new Event();
                event.setKey("ID", ""+((Signs)items.elementAt(listLcd.getSelectedIndex())).getId());
                controller.handleEvent(Event.SIGN_DETAIL, event);
            }
        }
    }
}
